package com.yyl.render.gesture

import android.content.Context
import android.view.GestureDetector
import android.view.GestureDetector.SimpleOnGestureListener
import android.view.MotionEvent

class MyGestureListener(context: Context, private val mGestureListener: SimpleGestureListener) :
    SimpleOnGestureListener() {
    private val SWIPE_MIN_DISTANCE = 100
    private val SWIPE_THRESHOLD_VELOCITY = 100

    private val mGestureDetector by lazy { GestureDetector(context, this) }

    fun onTouchEvent(event: MotionEvent?) {
        mGestureDetector.onTouchEvent(event)
    }

    override fun onFling(
        e1: MotionEvent,
        e2: MotionEvent,
        velocityX: Float,
        velocityY: Float
    ): Boolean {
        if (e1.x - e2.x > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
            mGestureListener.onSwipe(SwipeDirection.SWIPE_RIGHT)
            return true
        } else if (e2.x - e1.x > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
            mGestureListener.onSwipe(SwipeDirection.SWIPE_LEFT)
            return true
        }
        if (e1.y - e2.y > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
            mGestureListener.onSwipe(SwipeDirection.SWIPE_UP)
            return true
        } else if (e2.y - e1.y > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
            mGestureListener.onSwipe(SwipeDirection.SWIPE_DOWN)
            return true
        }
        return false
    }
}
